import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:hibrid_flutter/gen/assets.gen.dart';
import 'package:player/player.dart';
import 'package:player/video_view.dart';

import '../main.dart';
import '../my_route.dart';
import 'video_controller.dart';

class VideoList extends StatefulWidget {
  final bool isPlay;

  VideoList({this.isPlay  = true});

  @override
  _VideoListState createState() => _VideoListState();
}

class _VideoListState extends State<VideoList> {
  final url = 'asset/videos/test.flv';
  late VideoController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoController();
    _controller.init().then((value) => setState(() {}));
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, childAspectRatio: 3 / 4),
        itemCount: _controller.dataList?.length,
        itemBuilder: (BuildContext context, int index) {
          var gestureWidget = GestureDetector(
              onTap: () => onItemTap(index),
              child: Container(decoration: BoxDecoration(border: Border.all(color: const Color(0xfffef5ff),width: 1/MediaQueryData.fromWindow(window).devicePixelRatio,),),
                child: Stack(
                  children: [
                    AbsorbPointer(
                      absorbing: true,
                      child: _controller.dataList == null
                          ? Container() // 加载提示或骨架屏
                          : VideoView(
                          player: Player()
                            ..setLoop(0)
                            ..setCommonDataSource(
                                _controller.dataList![index].url,
                                type: SourceType.net,
                                showCover: !widget.isPlay,
                                autoPlay: widget.isPlay)),
                    ),
                    Positioned(
                      bottom: 10,
                      left: 6,
                      child: Row(
                        children: [
                          Image.asset(
                            Assets.images.play.path,
                            height: 18,
                            width: 16,
                          ),
                          const Padding(
                            padding: EdgeInsets.only(left: 4),
                            child: Text(
                              '3w+',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 14,
                                  decoration: TextDecoration.none),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),));
//          实际项目中，需要通过dateList[index]取出url
          return gestureWidget;
        });
  }

  onItemTap(int index) async {
    await myRoute.push(
        name: MyRoute.playerPage, args: _controller.dataList![index].url);
  }
}
